﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management.Automation;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Collections;

namespace BELT
{
    [Cmdlet("Get", "BrowserElementProperty")]
    public class GetBrowserElementPropertyCommand : Cmdlet
    {
        private Object _targetObject;
        private string _propertyName;
        [Parameter(Mandatory = true, ValueFromPipeline = true)]
        public Object TargetObject
        {
            get
            {
                return _targetObject;
            }
            set
            {
                PSObject psObj = (PSObject)value;
                _targetObject = psObj.BaseObject;
            }
        }
        [Parameter(Mandatory = true, ValueFromPipeline = false, Position = 0)]
        public string PropertyName
        {
            get
            {
                return _propertyName;
            }
            set
            {
                _propertyName = value;
            }
        }
        protected override void BeginProcessing()
        {
            base.BeginProcessing();
        }
        protected override void ProcessRecord()
        {
            base.ProcessRecord();
            Type objType = _targetObject.GetType();
            try
            {
                Object objResult = objType.InvokeMember(_propertyName, BindingFlags.GetProperty, null, _targetObject, null);
                WriteObject(objResult.ToString());
            }
            catch { 
                
            }
        }
        protected override void EndProcessing()
        {
            base.EndProcessing();
        }
    }
}
